package com.honghe.system.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.honghe.common.utils.DateUtils;
import com.honghe.common.utils.SecurityUtils;
import com.honghe.system.domain.SysAttendance;
import com.honghe.system.domain.SysAttendanceReapply;
import com.honghe.system.mapper.SysAttendanceMapper;
import com.honghe.system.mapper.SysAttendanceReapplyMapper;
import com.honghe.system.service.ISysAttendanceReapplyService;

/**
 * 补卡申请Service业务层处理
 * 
 * @author honghe
 */
@Service
public class SysAttendanceReapplyServiceImpl implements ISysAttendanceReapplyService 
{
    @Autowired
    private SysAttendanceReapplyMapper sysAttendanceReapplyMapper;
    
    @Autowired
    private SysAttendanceMapper sysAttendanceMapper;

    /**
     * 查询补卡申请
     * 
     * @param reapplyId 补卡申请主键
     * @return 补卡申请
     */
    @Override
    public SysAttendanceReapply selectSysAttendanceReapplyByReapplyId(Long reapplyId)
    {
        return sysAttendanceReapplyMapper.selectSysAttendanceReapplyByReapplyId(reapplyId);
    }

    /**
     * 查询补卡申请列表
     * 
     * @param sysAttendanceReapply 补卡申请
     * @return 补卡申请
     */
    @Override
    public List<SysAttendanceReapply> selectSysAttendanceReapplyList(SysAttendanceReapply sysAttendanceReapply)
    {
        return sysAttendanceReapplyMapper.selectSysAttendanceReapplyList(sysAttendanceReapply);
    }

    /**
     * 新增补卡申请
     * 
     * @param sysAttendanceReapply 补卡申请
     * @return 结果
     */
    @Override
    public int insertSysAttendanceReapply(SysAttendanceReapply sysAttendanceReapply)
    {
        sysAttendanceReapply.setCreateTime(DateUtils.getNowDate());
        return sysAttendanceReapplyMapper.insertSysAttendanceReapply(sysAttendanceReapply);
    }

    /**
     * 修改补卡申请
     * 
     * @param sysAttendanceReapply 补卡申请
     * @return 结果
     */
    @Override
    public int updateSysAttendanceReapply(SysAttendanceReapply sysAttendanceReapply)
    {
        sysAttendanceReapply.setUpdateTime(DateUtils.getNowDate());
        return sysAttendanceReapplyMapper.updateSysAttendanceReapply(sysAttendanceReapply);
    }

    /**
     * 批量删除补卡申请
     * 
     * @param reapplyIds 需要删除的补卡申请主键
     * @return 结果
     */
    @Override
    public int deleteSysAttendanceReapplyByReapplyIds(Long[] reapplyIds)
    {
        return sysAttendanceReapplyMapper.deleteSysAttendanceReapplyByReapplyIds(reapplyIds);
    }

    /**
     * 删除补卡申请信息
     * 
     * @param reapplyId 补卡申请主键
     * @return 结果
     */
    @Override
    public int deleteSysAttendanceReapplyByReapplyId(Long reapplyId)
    {
        return sysAttendanceReapplyMapper.deleteSysAttendanceReapplyByReapplyId(reapplyId);
    }
    
    /**
     * 提交补卡申请
     * 
     * @param sysAttendanceReapply 补卡申请
     * @return 结果
     */
    @Override
    public int applyReapply(SysAttendanceReapply sysAttendanceReapply)
    {
        // 设置用户ID
        sysAttendanceReapply.setUserId(SecurityUtils.getUserId());
        // 设置初始状态为待审批
        sysAttendanceReapply.setStatus("0");
        // 设置创建者和创建时间
        sysAttendanceReapply.setCreateBy(SecurityUtils.getUsername());
        sysAttendanceReapply.setCreateTime(DateUtils.getNowDate());
        
        // 查询是否已存在该日期的考勤记录
        SysAttendance attendance = new SysAttendance();
        attendance.setUserId(SecurityUtils.getUserId());
        attendance.setAttendanceDate(sysAttendanceReapply.getAttendanceDate());
        List<SysAttendance> attendanceList = sysAttendanceMapper.selectSysAttendanceList(attendance);
        
        // 如果存在考勤记录，则标记为补卡状态
        if (attendanceList != null && !attendanceList.isEmpty()) {
            SysAttendance existAttendance = attendanceList.get(0);
            existAttendance.setStatus("R");
            existAttendance.setUpdateBy(SecurityUtils.getUsername());
            existAttendance.setUpdateTime(DateUtils.getNowDate());
            sysAttendanceMapper.updateSysAttendance(existAttendance);
        }
        
        return sysAttendanceReapplyMapper.insertSysAttendanceReapply(sysAttendanceReapply);
    }
    
    /**
     * 审批补卡申请
     * 
     * @param reapplyId 补卡申请ID
     * @param status 审批状态
     * @param remark 审批备注
     * @return 结果
     */
    @Override
    public int approveReapply(Long reapplyId, String status, String remark) {
        // 查询补卡申请
        SysAttendanceReapply reapply = sysAttendanceReapplyMapper.selectSysAttendanceReapplyByReapplyId(reapplyId);
        if (reapply == null) {
            return 0;
        }
        
        // 更新补卡申请状态
        reapply.setStatus(status);
        reapply.setRemark(remark);
        reapply.setUpdateBy(SecurityUtils.getUsername());
        reapply.setUpdateTime(DateUtils.getNowDate());
        
        // 如果审批通过，更新考勤记录
        if ("1".equals(status)) {
            // 查询是否已存在该日期的考勤记录
            SysAttendance attendance = new SysAttendance();
            attendance.setUserId(reapply.getUserId());
            attendance.setAttendanceDate(reapply.getAttendanceDate());
            List<SysAttendance> attendanceList = sysAttendanceMapper.selectSysAttendanceList(attendance);
            
            if (attendanceList != null && !attendanceList.isEmpty()) {
                // 如果存在考勤记录，则更新考勤记录
                SysAttendance existAttendance = attendanceList.get(0);
                
                // 根据补卡类型更新签到或签退信息
                if ("in".equals(reapply.getReapplyType())) {
                    existAttendance.setClockInTime(reapply.getReapplyTime());
                    existAttendance.setClockInLocation(reapply.getReapplyLocation());
                } else if ("out".equals(reapply.getReapplyType())) {
                    existAttendance.setClockOutTime(reapply.getReapplyTime());
                    existAttendance.setClockOutLocation(reapply.getReapplyLocation());
                }
                
                existAttendance.setStatus("normal"); // 更新状态为正常
                existAttendance.setUpdateBy(SecurityUtils.getUsername());
                existAttendance.setUpdateTime(DateUtils.getNowDate());
                sysAttendanceMapper.updateSysAttendance(existAttendance);
            } else {
                // 如果不存在考勤记录，则创建一条新的考勤记录
                SysAttendance newAttendance = new SysAttendance();
                newAttendance.setUserId(reapply.getUserId());
                newAttendance.setAttendanceDate(reapply.getAttendanceDate());
                
                // 根据补卡类型设置签到或签退信息
                if ("in".equals(reapply.getReapplyType())) {
                    newAttendance.setClockInTime(reapply.getReapplyTime());
                    newAttendance.setClockInLocation(reapply.getReapplyLocation());
                } else if ("out".equals(reapply.getReapplyType())) {
                    newAttendance.setClockOutTime(reapply.getReapplyTime());
                    newAttendance.setClockOutLocation(reapply.getReapplyLocation());
                }
                
                newAttendance.setStatus("normal"); // 设置状态为正常
                newAttendance.setCreateBy(SecurityUtils.getUsername());
                newAttendance.setCreateTime(DateUtils.getNowDate());
                sysAttendanceMapper.insertSysAttendance(newAttendance);
            }
        }
        
        return sysAttendanceReapplyMapper.updateSysAttendanceReapply(reapply);
    }
} 